package com.zat.mes.andrmes.dofun.work.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseFragment
import com.zat.mes.andrmes.dofun.work.DayWorkRecordActivity
import com.zat.mes.andrmes.dofun.work.DayWorkRecordListActivity
import com.zat.mes.andrmes.dofun.work.adapter.DayProduceEmpListAdapter
import com.zat.mes.andrmes.dofun.work.vm.WorkRecordViewModel
import com.zat.mes.andrmes.event.WorkRecordEventMessage
import kotlinx.android.synthetic.main.fragment_day_p_plan_emplist.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * @author zy
 * @date :2020-02-25 15:38
 * @description:做工记录员工列表fragment
 */
class DayRecordEmpListFragment : BaseFragment() {

    private lateinit var mActivity: DayWorkRecordActivity
    private var workViewModel: WorkRecordViewModel? = null
    private lateinit var dayProduceEmpListAdapter: DayProduceEmpListAdapter
    private var classId: Long = 0

    companion object {
        private val EMPID = "empId"
        private val CLASSID = "classId"
        fun newInstance(empId: Long, classId: Long): DayRecordEmpListFragment {
            val fragment = DayRecordEmpListFragment()
            val args = Bundle()
            args.putLong(EMPID, empId)
            args.putLong(CLASSID, classId)
            fragment.arguments = args
            return fragment
        }
    }

    override fun getViewId(): Int {
        return R.layout.fragment_day_p_plan_emplist
    }

    override fun isOpenTitleBar(): Boolean {
        return false
    }

    override fun initView() {
    }

    override fun initData() {
        mActivity = context as DayWorkRecordActivity
        EventBus.getDefault().register(this);
        dayProduceEmpListAdapter = DayProduceEmpListAdapter(R.layout.item_ry_day_plan_emp,true)
        recyclerEmpList.layoutManager = LinearLayoutManager(context)
        recyclerEmpList.adapter = dayProduceEmpListAdapter
        classId = arguments?.getLong(CLASSID)!!

        dayProduceEmpListAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, DayWorkRecordListActivity::class.java)
            intent.putExtra("empId", dayProduceEmpListAdapter.data.get(position).empid)
            intent.putExtra("empName", dayProduceEmpListAdapter.data.get(position).empname)
            intent.putExtra("day", mActivity.selectedDate)
            startFragmentActivity(intent)
        }
        initModel()
    }

    private fun initModel() {
        workViewModel = ViewModelProvider(this).get(WorkRecordViewModel::class.java)
        workViewModel?.getEmpList()?.observe(this, Observer {
            closeLoading()
            dayProduceEmpListAdapter.setNewData(it)
        })
        workViewModel?.loadClassEmpList(1, Int.MAX_VALUE, classId, mActivity.selectedDate)
        showLoading()
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onDateChangeEvent(event: WorkRecordEventMessage?) {
        Log.e("eventbus", "1===")
        if (workViewModel != null) {
            workViewModel?.loadClassEmpList(1, Int.MAX_VALUE, classId, mActivity.selectedDate)
            showLoading()
        }
    }

    override fun onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView()
    }
}